﻿using System;
using YArchitech.HCLoad.Utils;
using Microsoft.Win32;

namespace YArchitech.HCLoad.Controls
{
	public class HcRegistry
	{
		public static bool GetRegValue(string propName, ref string sValue)
		{
			if (HcRegistry.SubKey == null || HcRegistry.SubKey.GetValue(propName) == null)
			{
				return false;
			}
			sValue = HcRegistry.SubKey.GetValue(propName).ToString();
			return true;
		}

		public static bool GetRegValue(string propName, ref int nValue)
		{
			string empty = string.Empty;
			if (HcRegistry.GetRegValue(propName, ref empty))
			{
				nValue = Convert.ToInt32(empty);
				return true;
			}
			return false;
		}

		public static bool GetRegValue(string propName, ref double dValue)
		{
			string empty = string.Empty;
			if (HcRegistry.GetRegValue(propName, ref empty))
			{
				if (HcUtils.IsNum(empty))
				{
					dValue = Convert.ToDouble(empty);
				}
				else
				{
					dValue = 0.0;
				}
				return true;
			}
			return false;
		}

		public static bool SetRegValue(string name, string tovalue)
		{
			HcRegistry.SubKey = HcRegistry.KeyRoot.CreateSubKey(HcRegistry.sSubKey);
			if (HcRegistry.SubKey == null)
			{
				return false;
			}
			HcRegistry.SubKey.SetValue(name, tovalue);
			return true;
		}

		public static bool SetRegValue(string name, int tovalue)
		{
			HcRegistry.SubKey = HcRegistry.KeyRoot.CreateSubKey(HcRegistry.sSubKey);
			if (HcRegistry.SubKey == null)
			{
				return false;
			}
			HcRegistry.SubKey.SetValue(name, tovalue);
			return true;
		}

		public static bool SetRegValue(string name, double tovalue)
		{
			HcRegistry.SubKey = HcRegistry.KeyRoot.CreateSubKey(HcRegistry.sSubKey);
			if (HcRegistry.SubKey == null)
			{
				return false;
			}
			HcRegistry.SubKey.SetValue(name, tovalue);
			return true;
		}

		private static string sSubKey = "Software\\Hongye\\HcLoad\\HcRevitKernel";

		private static RegistryKey KeyRoot = Registry.CurrentUser;

		private static RegistryKey SubKey = HcRegistry.KeyRoot.OpenSubKey(HcRegistry.sSubKey, true);
	}
}
